<?php

use common\grid\EnumColumn;
use common\models\Article;
use common\models\ArticleCategory;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\web\JsExpression;



use yii\helpers\Url;
use kartik\editable\Editable;
use kartik\export\ExportMenu;
use kartik\dynagrid\DynaGrid;
use kartik\grid\GridView;
/**
 * @var $this         yii\web\View
 * @var $searchModel  backend\modules\content\models\search\ArticleSearch
 * @var $dataProvider yii\data\ActiveDataProvider
 */

$this->title = Yii::t('backend', 'Articles');

$this->params['breadcrumbs'][] = $this->title;

?>

<p>
    <?php echo Html::a(
        Yii::t('backend', 'Create {modelClass}', ['modelClass' => 'Article']),
        ['create'],
        ['class' => 'btn btn-success']) ?>
</p>

<div>


<?php

$columns = [
    ['class' => 'yii\grid\SerialColumn'],

    [
        'class' => '\kartik\grid\CheckboxColumn',
        'options' => ['style' => 'width: 5%'],
    ],

    [
        'class' => '\kartik\grid\ActionColumn',
        'options' => ['style' => 'width: 5%'],
        'template' => '{update} {delete}',
        'deleteOptions' => ['title' => 'delete', 'data-toggle' => 'tooltip', 'data-confirm' => 'confirm delete?'],
    ],
//    [
//        'attribute' => 'id',
//        'options' => ['style' => 'width: 5%'],
//    ],
    [
        'attribute' => 'slug',
        'options' => ['style' => 'width: 15%'],
    ],
    [
        'attribute' => 'title',
        'value' => function ($model) {
            return Html::a($model->title, ['update', 'id' => $model->id]);
        },
        'format' => 'raw',
    ],
    [
        'attribute' => 'category_id',
        'options' => ['style' => 'width: 10%'],
        'value' => function ($model) {
            return $model->category ? $model->category->title : null;
        },
        'filter' => ArrayHelper::map(ArticleCategory::find()->all(), 'id', 'title'),
    ],
    [
        'attribute' => 'created_by',
        'options' => ['style' => 'width: 10%'],
        'value' => function ($model) {
            return $model->author->username;
        },
    ],
    [
        'class' => EnumColumn::class,
        'attribute' => 'status',
        'options' => ['style' => 'width: 10%'],
        'enum' => Article::statuses(),
        'filter' => Article::statuses(),
    ],
    [
        'attribute' => 'published_at',
        'options' => ['style' => 'width: 10%'],
        'format' => 'datetime',

    ],


];
//echo '<pre>';
//print_r($columns);die;

$export_file_name =  'article_'.date("d-m-Y-H-i-s");
$export_columns = $columns;
foreach ($export_columns as $k => &$v) {
    if (isset($v['order'])) unset($v['order']);
}

$fullExportMenu = \backend\widgets\Export::widget([
    'dataProvider' => $dataProvider,
    'columns' => [

            'id',
            'slug',
            'title',
            'category_id',
        'created_by',

    ],
    'fontAwesome' => true,
    'noExportColumns' => [1],
//            'batchSize' => 10,

    //'target' => '_blank',



    'showConfirmAlert' => true,
    'clearBuffers' => true,
    'timeout' => 0,
//    'batchSize' => 100,
//    'target' => \kartik\export\ExportMenu::TARGET_BLANK,
    'stream' => false,
    'linkPath' => '/tmp',
    'folder' => "@webroot/tmp",
    'filename' => $export_file_name,

//            'exportConfig' => [
//                ExportMenu::FORMAT_TEXT => false,
//                ExportMenu::FORMAT_PDF => false,
//                ExportMenu::FORMAT_HTML => false,
//                ExportMenu::FORMAT_CSV => false,
//            ],
]);

echo DynaGrid::widget([
    'columns' => $columns,

    'storage' => DynaGrid::TYPE_COOKIE,
    'theme' => 'panel-default',
    'gridOptions' => [
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
       // 'panel' => ['heading' => '<h3 class="panel-title">Library</h3>'],
        'toolbar' => [
            ['content' => '{dynagrid}',],
            $fullExportMenu
        ],
        //  'perfectScrollbar' => true,
        'perfectScrollbarOptions' => [
            'wheelPropagation' => true,
        ],
        // 'floatHeader' => true,
        //'showPageSummary' => true,
    ],
    'showPersonalize' => true,
    'options' => ['id' => 'dynagrid-1'], // a unique identifier is important
]);

?>
</div>
